//
//  LifeBillView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/11/5.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol LifeBillViewDelegate {
    @objc optional func selectBillType(title:String) //选择账单类型
}

class LifeBillView: UIView {
    
    weak var delegate : LifeBillViewDelegate?
    
    fileprivate let cellId = "lifeBillCell"
    fileprivate let dataArray = [houseLoan,carLoan,rent,water,electricity,gasCharges,propertyFee,mobilePhoneCharges,broadbandFee]
    fileprivate let images = ["house_loan","car","lend_out","electric","water","gas","property","phone","internet"]
    
    var collectionView : UICollectionView!

    init() {
        super.init(frame: .zero)
        
        self.backgroundColor = UIColor.white
        
        let redView = UIView()
        redView.backgroundColor = UIColor.red
        redView.layer.cornerRadius = RATIO(maxNum: 2)
        redView.layer.masksToBounds = true
        self.addSubview(redView)
        
        let titleLabel = UILabel()
        titleLabel.text = lifeBill
        titleLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 20))
        self.addSubview(titleLabel)
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = RATIO_H(maxNum: 20)
        layout.itemSize = .init(width: ScreenW/3, height: RATIO_H(maxNum: 80))
        
        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.clear
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(AddLifeBillCell.self, forCellWithReuseIdentifier: cellId)
        self.addSubview(collectionView)
        
        let lineView = UIView()
        lineView.backgroundColor = UIColor.lineColor()
        self.addSubview(lineView)
        
        redView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(RATIO_H(maxNum: 20))
            make.left.equalToSuperview().offset(RATIO(maxNum: 20))
            make.width.equalTo(RATIO(maxNum: 4))
            make.height.equalTo(RATIO_H(maxNum: 20))
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(redView)
            make.left.equalTo(redView.snp.right).offset(RATIO(maxNum: 10))
            make.width.greaterThanOrEqualTo(10)
            make.height.equalTo(RATIO_H(maxNum: 30))
        }
        
        lineView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
            make.top.equalTo(redView.snp.bottom).offset(RATIO_H(maxNum: 20))
        }
        
        collectionView.snp.makeConstraints { (make) in
            make.top.equalTo(lineView.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
        
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    

}

extension LifeBillView : UICollectionViewDelegate,UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as? AddLifeBillCell
        cell?.imageView.image = UIImage.init(named: images[indexPath.row])
        cell?.titleLabel.text = dataArray[indexPath.row]
        return cell!
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
        
        self.delegate?.selectBillType!(title: dataArray[indexPath.row])
    }
    
}

